package com.catal.framework.data;

import java.util.List;


/**
 * 查询条件类 
 * <li>1.普通条件<b> conditionType = "1"</b>，条件只有列名(leftItem), 操作类型或操作符(operType),条件值1(value1),条件值2(value2).  eg: t.a > 0</li>
 * <li>2.And条件<b> conditionType = "2"</b> ，左边条件(leftCondition)和右边条件(rightCondition)</li>
 * <li>3.Or条件 <b>conditionType = "3" </b>，左边条件(leftCondition)和右边条件(rightCondition)</li>
 * @author chensr
 *
 */
public class Condition {
	/**
	 * 条件表达式左边项
	 */
	private String leftItem;
	
	/**
	 * 操作类型或操作符
	 */
	private String operType;
	
	/**
	 * 条件表达式右边值
	 */
	private String value1;
	
	/**
	 * 条件表达式右边值2
	 */
	private String value2;
	
	/**
	 * 左边条件
	 */
	private Condition leftCondition;
	
	/**
	 * 右边条件
	 */
	private Condition rightCondition;
	
	/**
	 * 条件类型
	 * 1-普通Condition,2-And Condition 3-Or Condition
	 */
	private String conditionType; // 
	
	public Condition() {
		super();
	}
	
	/**
	 * 无值构造方法  is null、is not null
	 * @param leftItem 左边表达式（列）
	 * @param operType 操作方式(操作符)
	 */
	public Condition(String leftItem, String operType) {
		super();
		this.leftItem = leftItem;
		this.operType = operType;
	}

	/**
	 * 单值值构造方法  =、 >、 <、 <=、>=、 like 
	 * @param leftItem 左边表达式（列）
	 * @param operType 操作方式(操作符)
	 * @param value1	值一
	 */
	public Condition(String leftItem, String operType, String value1) {
		super();
		this.leftItem = leftItem;
		this.operType = operType;
		this.value1 = value1;
		this.conditionType = "1";
	}

	/**
	 * 双值构造方法
	 * @param leftItem 左边表达式（列）
	 * @param operType 操作方式(操作符)
	 * @param value1	值一
	 * @param value2	值二
	 */
	public Condition(String leftItem, String operType, String value1,
			String value2) {
		super();
		this.leftItem = leftItem;
		this.operType = operType;
		this.value1 = value1;
		this.value2 = value2;
		this.conditionType = "1";
	}

	/**
	 * 子类中添加左边条件下
	 * @param condition
	 */
	public void setLeftCondition(Condition leftCondition){
		this.leftCondition = leftCondition;
	}
	
	public String getLeftItem() {
		return leftItem;
	}
	public void setLeftItem(String leftItem) {
		this.leftItem = leftItem;
	}
	public String getOperType() {
		return operType;
	}
	public void setOperType(String operType) {
		this.operType = operType;
	}
	public String getValue1() {
		return value1;
	}
	public void setValue1(String value1) {
		this.value1 = value1;
	}
	public String getValue2() {
		return value2;
	}
	public void setValue2(String value2) {
		this.value2 = value2;
	}

	public Condition getRightCondition() {
		return rightCondition;
	}

	public void setRightCondition(Condition rightCondition) {
		this.rightCondition = rightCondition;
	}

	public Condition getLeftCondition() {
		return leftCondition;
	}

	
	public String toString(){
		if(ConditionOperSign.SIMPLE_CONDITION_TYPE.equals(conditionType)){ //普通condition
			return toConditionString();
		}else if(ConditionOperSign.And_CONDITION_TYPE.equals(conditionType)){//And
			return toAndString();
		}else if(ConditionOperSign.OR_CONDITION_TYPE.equals(conditionType)){
			return toOrString();
		}
		return "";
	}
	
	public String toConditionString(){
		String str = getLeftItem()+ " ";
		if(ConditionOperSign.EQUAL.equals(operType)){
			str += "=";
			if(value1 instanceof String){
				str+="'"+value1+"'";
			}else{
				str += value1;
			}
		}else if(ConditionOperSign.LIKE.equals(operType)){
			str += " like ";
			str +="'%"+value1+"%'";
		}else if(ConditionOperSign.LT.equals(operType)){
			str += "<";
			if(value1 instanceof String){
				str+="'"+value1+"'";
			}else{
				str += value1;
			}
		}else if(ConditionOperSign.GT.equals(operType)){
			str += ">";
			if(value1 instanceof String){
				str+="'"+value1+"'";
			}else{
				str += value1;
			}
		}else if(ConditionOperSign.LE.equals(operType)){
			str += "<=";
			if(value1 instanceof String){
				str+="'"+value1+"'";
			}else{
				str += value1;
			}
		}else if(ConditionOperSign.GE.equals(operType)){
			str += ">=";
			if(value1 instanceof String){
				str+="'"+value1+"'";
			}else{
				str += value1;
			}
		}else if(ConditionOperSign.BETWEEN.equals(operType)){
			if(value1 instanceof String){
				str+= "BETWEEN '" + value1 + "' AND '" + value2 + "'";
			}else{
				str+= "BETWEEN " + value1 + " AND " + value2;
			}
		}else if(ConditionOperSign.IS_NULL.equals(operType)){
			str += " is null";
		}else if(ConditionOperSign.IS_NOT_NULL.equals(operType)){
			str += " IS NOT NULL ";
		}
		
		return str;
	}
	
	public String toOrString(){
		StringBuffer buffer = new StringBuffer("((");
		buffer.append(this.leftCondition.toString() + ")");
		buffer.append(" OR ").append("("+rightCondition+")");
		buffer.append(")");
		return buffer.toString();
	}
	
	public String toAndString(){
		StringBuffer buffer = new StringBuffer(this.leftCondition.toString());
		buffer.append(" AND ").append(rightCondition);
		return buffer.toString();
	}

	public String getConditionType() {
		return conditionType;
	}

	public void setConditionType(String conditionType) {
		this.conditionType = conditionType;
	}
}
